<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-07-30 20:02:30
 * @LastEditTime: 2020-02-28 11:25:38
 * @LastEditors: Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/vant.css">
    <link rel="stylesheet" type="text/css" href="css/base.css" />
    <script type="text/javascript" src="script/vue.min.js"></script>
    <script type="text/javascript" src="script/vant.min.js"></script>
    <script type="text/javascript" src="script/base.js"></script>
    <script type="text/javascript" src="script/md5.min.js"></script>
    <style type="text/css">
        .box {
            background-image: url('image/bgIMG.png');
            background-repeat: no-repeat;
            background-size: 100% 100%;
        }

        .login-box {
            width: 40%;
            height: auto;
            margin: auto;
            /* margin-top: 30vh; */
            padding: 30px;
            opacity: 1;
            background-color: rgba(255, 255, 255, 0.4);
            border-radius: 10px;
            display: flex;
            flex-direction: column;
            position: relative;
        }

        .op {
            position: absolute;
            top: -40px;
            left: -50px;
            font-weight: 700;
            color: #cececf;
            font-size: 60px;
            letter-spacing: -2px;
        }

        .van-cell {
            border-radius: 8px;
            margin-bottom: 10px;
            line-height: 18px;
        }

        .van-field__label {
            margin-right: 0;
            width: 3em;
        }

        .van-checkbox {
            margin-left: auto;
            margin-bottom: 10px;
        }

        .van-button {
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="box">
            <div class="login-box">
                <van-field v-model="formData.P_Name" label="账号：" :disabled="buttonShow" placeholder="请输入账号"></van-field>
                <van-field type="password" v-model="formData.P_Password" :disabled="buttonShow" label="密码："
                    placeholder="请输入密码"></van-field>
                <van-field v-model="formData.className" label="车间：" placeholder="请选择车间" readonly is-link
                    @click="handlerClass"></van-field>
                <van-checkbox v-model="formData.checked" shape="square">记住密码</van-checkbox>
                <van-button type="info" size="normal" @click="loginApp" :disabled="buttonShow">登 &nbsp &nbsp &nbsp &nbsp
                    &nbsp &nbsp &nbsp
                    &nbsp 录</van-button>
            </div>
        </div>

        <van-action-sheet v-model="classShow" :actions="classActions" @select="onSelectClass">
        </van-action-sheet>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {
        api.setScreenOrientation({
            orientation: 'auto_landscape'
        });
        api.addEventListener({
            name: 'auto_landscape_login'
        }, function (ret) {
            api.setScreenOrientation({
                orientation: 'auto_landscape'
            });
        })

        vm = new Vue({
            el: '#app',
            data: {
                formData: {
                    P_Name: '',
                    P_Password: '',
                    checked: '',
                    className: '',
                    classID: ''
                },
                classShow: false,
                classActions: [],
                buttonShow: false
            },
            created: function () {
                var that = this;
                console.log(APP.serverIP);
            },
            mounted() {
                APP.keyBoardChange()
                this.checkUpdate()
            },
            methods: {
                handlerClass() {
                    var that = this
                    APP.ajax('getWorkShop', {}, function (res) {
                        console.log('res', JSON.stringify(res))
                        that.classActions = res.ds
                        that.classShow = true
                    })
                },
                onSelectClass(val) {
                    var that = this
                    that.formData.className = val.name
                    that.formData.classID = val.value
                    that.classShow = false
                    window.localStorage.setItem('CS', JSON.stringify(val.name))

                },

                checkUpdate() {
                    var that = this
                    var userInfo = JSON.parse(window.localStorage.getItem('userInfo'))
                    if (userInfo && userInfo.P_Password) {
                        that.formData.P_Name = userInfo.UserCode
                        that.formData.P_Password = userInfo.P_Password
                        that.formData.className = userInfo.className
                        that.formData.classID = userInfo.classID
                        that.formData.checked = true
                        // that.loginApp()
                    } else {
                        that.formData.P_Name = userInfo ? userInfo.UserCode : ''
                    }
                    window.localStorage.setItem('deviceModel', JSON.stringify(api.deviceModel))

                    var currentVersion0 = api.appVersion.split('.')[0]
                    var currentVersion1 = api.appVersion.split('.')[1]
                    var currentVersion2 = api.appVersion.split('.')[2]
                    // console.log('appVersion', JSON.stringify(api.appVersion))
                    APP.ajax('ZS_FILELIST', {}, function (res) {
                        // window.localStorage.setItem('apk', JSON.stringify(res))

                        var F_Ver0 = res.ds[0].F_Ver.split('.')[0]
                        var F_Ver1 = res.ds[0].F_Ver.split('.')[1]
                        var F_Ver2 = res.ds[0].F_Ver.split('.')[2]
                        // console.log(JSON.stringify(Number(currentVersion0) < Number(F_Ver0) || Number(currentVersion1) < Number(F_Ver1) || Number(currentVersion2) < Number(F_Ver2)))
                        if (Number(currentVersion0) < Number(F_Ver0) || Number(currentVersion1) < Number(F_Ver1) || Number(currentVersion2) < Number(F_Ver2)) {
                            api.confirm({
                                title: '有新版本，是否下载并安装',
                                msg: '版本号：' + res.ds[0].F_Ver,
                                buttons: ['立即更新', '取消']
                            }, function (ret, err) {
                                if (ret.buttonIndex == 1) {
                                    if (api.systemType == 'android') {
                                        that.buttonShow = true
                                        api.download({
                                            url: res.ds[0].D_Url,
                                            report: true
                                        }, function (downloadRet, err) {
                                            // alert('downloadRet' + JSON.stringify(downloadRet))
                                            if (err) {
                                                api.hideProgress();
                                                api.toast({
                                                    msg: '下载失败请重试!',
                                                    duration: 2000
                                                })
                                            } else if (downloadRet.state == 0) {
                                                api.showProgress({
                                                    title: downloadRet.percent + '%',
                                                    modal: false
                                                });
                                            } else if (downloadRet && downloadRet.state == 1) {
                                                // alert('state1' + JSON.stringify(downloadRet))
                                                api.hideProgress();
                                                api.toast({
                                                    msg: '下载完成!',
                                                    duration: 2000
                                                })
                                                api.installApp({
                                                    appUri: 'file://' + downloadRet.savePath
                                                });
                                                that.buttonShow = false

                                            }
                                        })
                                    }
                                }
                                else {
                                    // that.checkUpdate()
                                }



                            });
                        }
                    })
                },

                loginApp() {
                    var that = this
                    var show = true
                    if (!that.formData.className) {
                        show = false
                        api.toast({
                            msg: '请选择车间'
                        })
                    }

                    if (show) {
                        var obj = {
                            P_Name: that.formData.P_Name,
                            P_Password: md5(that.formData.P_Password),
                        }
                        APP.ajax('PC_Login', obj, function (res) {
                            // console.log(JSON.stringify(res))
                            if (res.ds[0].status == 200) {
                                if (that.formData.checked) {
                                    res.ds[0].P_Password = that.formData.P_Password
                                } else {
                                    res.ds[0].P_Password = ''
                                }
                                res.ds[0].className = that.formData.className
                                res.ds[0].classID = that.formData.classID

                                window.localStorage.setItem('userInfo', JSON.stringify(res.ds[0]))
                                api.toast({
                                    msg: res.ds[0].msg
                                })
                                api.openWin({
                                    name: 'index',
                                    url: 'html/index.html'
                                })
                            } else {
                                api.toast({
                                    msg: res.ds[0].msg
                                })
                            }
                        })

                    }

                }
            }
        })

    };
</script>

</html>